home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gigarom 1
/
Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso
/
FILES
/
BBS
/
SECOND_SIGHT
/
GEnie Cleaner.cpt
/
Text Utils.p
< prev
Wrap
Text File
|
1991-10-18
|
4KB
|
158 lines
unit TextUtils;
interface
function AtEOF (FileRefNum: integer): boolean;
function Wr (FileRefNum: integer; TheMessage: str255): OSErr;
function WrLn (FileRefNum: integer; TheMessage: str255): OSErr;
function ReadALine (FileRefNum: integer; var TheMessage: str255): OSErr;
function RPadString (theString: str255; theLength: integer): str255;
function LPadString (theString: str255; theLength: integer): str255;
function ReplaceInString (bigString, oldString, newString: str255): str255;
function GetValue (aString: str255): longint;
function ValueToString (aNum: longint): str255;
implementation
{ ------------------------------------------------------ }
function AtEOF; {(FileRefNum: integer): boolean}
var
currPos, eofPos: LongInt;
myErr: OSErr;
begin
myErr := GetFPos(FileRefNum, currPos);
if myErr = NoErr then
myErr := GetEOF(FileRefNum, eofPos);
if myErr = NoErr then
AtEOF := currPos = eofPos
else
AtEOF := true {if there was an error, report end of file}
end;
{ ------------------------------------------------------ }
function Wr; {(FileRefNum: integer; TheMessage: str255): OSErr}
{ Function writes string to text file, returns error code }
var
TheLength: longint;
begin
TheLength := length(TheMessage);
Wr := FSWrite(FileRefNum, TheLength, Pointer(ord(@TheMessage) + 1));
end;
{----------------------------------------------------------------- }
function WrLn; {(FileRefNum: integer; TheMessage: str255): OSErr}
const
ENDLINE = chr(13);
begin
WrLn := Wr(FileRefNum, concat(TheMessage, ENDLINE))
end;
{----------------------------------------------------------------- }
function ReadALine; {(FileRefNum: integer; var TheMessage: str255): OSErr}
var
myPB: ParamBlockRec;
myString: Str255;
begin
myString := '';
myPB.ioCompletion := nil;
myPB.ioRefNum := FileRefNum;
myPB.ioBuffer := Pointer(@myString[1]);
myPB.ioReqCount := 255;
myPB.ioPosMode := 3456; {ASCII 13*256+128}
myPB.ioPosOffset := 0; {ignored}
ReadALine := PBRead(@myPB, False);
if (myString[myPB.ioActCount] = chr(13)) then
myString[0] := char(myPB.ioActCount - 1) {Drop CR}
else
myString[0] := char(myPB.ioActCount);
TheMessage := myString
end;
{----------------------------------------------------------------- }
function LPadString; {(theString: str255; theLength: integer): str255 }
{ Left justifies a string to theLength }
begin
if length(theString) > theLength then
theString := copy(theString, 1, theLength);
while length(theString) < theLength do
theString := concat(theString, ' ');
LPadString := theString
end;
{----------------------------------------------------------------- }
function RPadString; {(theString: str255; theLength: integer): str255 }
{ Right justifies a string to theLength }
begin
if length(theString) > theLength then
theString := copy(theString, 1, theLength);
while length(theString) < theLength do
theString := concat(' ', theString);
RPadString := theString
end;
{----------------------------------------------------------------- }
function ReplaceInString; {(bigString, oldString, newString: str255): str255 }
{ Replaces oldString with newString within bigString, returns new bigString }
var
beginString, endString: str255;
begin
ReplaceInString := bigString;
if (pos(oldString, bigString) > 0) then
begin
beginString := copy(bigString, 1, pred(pos(oldString, bigString)));
endString := copy(bigString, pos(oldString, bigString) + length(oldString), 255);
if ((length(BeginString) + length(newString) + length(EndString)) < 255) then
ReplaceInString := concat(beginString, newString, endString)
end
end;
{----------------------------------------------------------------- }
function GetValue; {(aString: str255): longint}
var
tempLong: longint;
begin
StringToNum(aString, tempLong);
GetValue := tempLong
end;
{----------------------------------------------------------------- }
function ValueToString; {(aNum: longint): str255}
var
aString: str255;
begin
NumToString(aNum, aString);
ValueToString := aString
end;
{----------------------------------------------------------------- }
end.